
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/water_fall/toast.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:photo_view/photo_view.dart';
List<String> _mImageUris;
int mIndex;
bool saving = false;

class ViewLocalImage extends StatefulWidget{
  ViewLocalImage(List<String> imageUris,int index){
    _mImageUris = imageUris;
    mIndex = index;
  }
  @override
  State<StatefulWidget> createState() {
    return ViewImageState();
  }

}

class ViewImageState extends State{
  bool showDownloadButton;
  @override
  void initState() {
    showDownloadButton = false;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          backgroundColor: Colors.black,
          body: Stack(
            children: <Widget>[
              Positioned(
                left: 0,
                top: 0,
                bottom: 0,
                right: 0,
                child: PageView.builder(
                  itemBuilder: (context,index) {
                    return Container(
                      constraints: BoxConstraints.expand(
                        height: MediaQuery.of(this.context).size.height,
                      ),
                      child: Container(
                          alignment: Alignment.center,
                          child: GestureDetector(
                            onLongPress: () async {
                              setState(() {
                                showDownloadButton = true;
                              });
                            },
                            onTap: (){
                              Navigator.of(this.context).pop();
                            },
                            child: PhotoView(
                              enableRotation: true,
                              imageProvider: AssetImage(_mImageUris[index]),
                              minScale: 0.1,
                            ),
                          )
                      ),
                    );
                  },
                  itemCount: _mImageUris.length,
                  scrollDirection: Axis.horizontal,
                  controller: PageController(
                    initialPage: mIndex,
                  ),
                  physics: BouncingScrollPhysics(),
                  onPageChanged: (index){
                    mIndex = index;
                  },
                ),
              ),
              Positioned(
                right: 20,
                top: 36,
                child: IconButton(icon: Icon(Icons.close,size: 28,color: Colors.white,),
                    onPressed: (){
                      Navigator.of(this.context).pop();
                    }
                ),
              ),
              Positioned(
                left: 20,
                top: 36,
                child: Offstage(
                  offstage: !showDownloadButton,
                  child: RaisedButton(
                      onPressed: (){
                        setState(() {
                          showDownloadButton = false;
                        });
                        _save(context);
                      },
                      color:Colors.transparent,
                      child: Text("下载图片",style: TextStyle(color: Colors.white),
                      )
                  ),
                ),
              )
            ],
          )
      ),
    );
  }

  static _save(BuildContext context) async {
    if(!saving) {
      saving = true;
      try{
        final result = await ImageGallerySaver.saveFile(_mImageUris[mIndex]);
        Toast.toast(context,msg: "已保存到${result.toString()}",showTime: 3000,position: ToastPosition.bottom);
      } on Exception{
        Toast.toast(context,msg: "保存失败",showTime: 3000,position: ToastPosition.bottom);
        saving = false;
      } finally{
        saving = false;
      }

    } else {
      Toast.toast(context,msg: "正在保存其他文件...",showTime: 3000,position: ToastPosition.bottom);
    }
  }
}
